type str = {
    emote: {   //表情
        T:{  
            id:number
            url:string
            text:string
        }
    }
    jump_url:{//高亮词汇
        T:{
            title:string
            p_url:string
        }
    }
    message:string
} 

//感觉有些不成熟  暂定

/**
 * 将string中的存在的HTML内容转换成真实的HTML节点
 * @param node 需要添加到的节点
 * @param str 需要转换的字符串
 */
export function emotoToHtml(node:HTMLElement,content:str){
    const regex = /\[[^\]]+\]/g
    const matches =  content.message.match(regex)       
    const seaArr = content.message.split(regex)
    if (matches){
        seaArr.forEach((el,index) => {
            if (Object.keys(content.jump_url).length) {
                hasJumpUrl(node,el,content.jump_url)
            } else {
                const span = document.createElement('span')
                span.textContent = el
                node.appendChild(span)
            }
            if (matches[index]) {
                const imgObj = content.emote[matches[index]]
                if (imgObj) {
                    const img = document.createElement('img')
                    img.src = imgObj.url
                    img.alt = '资源加载出错'
                    img.style.width = '20px'
                    img.style.height = '20px'
                    img.style.padding = '0 3px'
                    node.appendChild(img)
                }
            }
        })        
    } else {
        if (Object.keys(content.jump_url).length) {
            hasJumpUrl(node,content.message,content.jump_url)
        } else {
            const span = document.createElement('span')
            span.textContent = content.message
            node.appendChild(span)
        }
    }
}

        
function hasJumpUrl(node:HTMLElement,val:string,jump:object) {
    const keyArr = Object.keys(jump)
    let regx = ''
    keyArr.forEach((el,index) => {
        if (index > 0)   regx = regx + '|' + el 
        else  regx = el
    })
    let keyRegExp = new RegExp(regx,'g')
    const arr = val.split(keyRegExp)
    arr.forEach((el,index) => {
        const span = document.createElement('span')
        span.textContent = el
        node.appendChild(span)
        if (keyArr[index]) {
            const a = document.createElement('a')
            a.textContent = keyArr[index]
            a.href = '#'
            a.style.color = '#008AC5'
            node.appendChild(a)

            const img = document.createElement('img')
            img.src = 'src/\assets/\png/\searchBlue.png'
            img.style.width = '15px'
            img.style.height = '15px'
            node.appendChild(img)
        }
    })
}